using Google.Protobuf;

namespace muduoDotNet
{
    /// <summary>
    /// 协议编解码器接口，定义消息的编码和解码规范
    /// </summary>
    public interface IProtocolCodec
    {
        /// <summary>
        /// 将消息对象编码为字节数组
        /// </summary>
        /// <param name="message">要编码的消息对象</param>
        /// <returns>编码后的字节数组</returns>
        byte[] Encode(IMessage message);

        /// <summary>
        /// 从缓冲区中解码消息
        /// </summary>
        /// <typeparam name="T">消息类型</typeparam>
        /// <param name="buffer">数据缓冲区</param>
        /// <param name="message">解码出的消息</param>
        /// <returns>如果成功解码返回true，否则返回false</returns>
        bool TryDecode<T>(Buffer buffer, out T? message) where T : class, IMessage<T>, new();

        /// <summary>
        /// 检查缓冲区中是否包含完整的消息
        /// </summary>
        /// <param name="buffer">数据缓冲区</param>
        /// <returns>如果包含完整消息返回消息长度，否则返回-1</returns>
        int CheckMessageComplete(Buffer buffer);

        /// <summary>
        /// 获取消息头长度（用于长度前缀）
        /// </summary>
        int HeaderLength { get; }
    }

    /// <summary>
    /// 协议消息回调委托
    /// </summary>
    /// <typeparam name="T">消息类型</typeparam>
    /// <param name="connection">TCP连接</param>
    /// <param name="message">解码后的消息</param>
    public delegate void ProtocolMessageCallback<T>(TcpConnection connection, T message) where T : class, IMessage;

    /// <summary>
    /// 协议消息处理器接口
    /// </summary>
    public interface IProtocolMessageHandler
    {
        /// <summary>
        /// 处理接收到的协议消息
        /// </summary>
        /// <param name="connection">TCP连接</param>
        /// <param name="message">消息对象</param>
        void HandleMessage(TcpConnection connection, IMessage message);
    }
}